#include "widget.h"
#include <QMessageBox>
#include <QApplication>
#include <QJsonDocument>
#include <QRegExpValidator>
#include <QDebug>
#include <QColor>
#include <QVariant>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    // 设置界面
    // 大小为固定宽度和高度
    this->setFixedSize(1111, 777);
    // 设置界面图标及名称
    this->setWindowTitle("机械臂控制端");
    this->setWindowIcon(QIcon("./icon/RoboticArm.png"));
    this->setStyleSheet("background-color: #0A0D35;");
    //布局
    QHBoxLayout *SetAll = new QHBoxLayout();//水平布局
    QHBoxLayout *SetRightDown = new QHBoxLayout();
    QHBoxLayout *SetleftDown = new QHBoxLayout();
    QVBoxLayout *SetLeft = new QVBoxLayout();//垂直布局
    QVBoxLayout *SetRight = new QVBoxLayout();

    //模块一---通信设置
    QGroupBox *CommunicationBox = new QGroupBox("Communication settings");
    SetLeft->addWidget(CommunicationBox);
    //模块二---控制模式
    QGroupBox *OperationBox = new QGroupBox("Mode of operation");
    SetleftDown->addWidget(OperationBox);
    //模块三---末端执行器控制
    QGroupBox *EndControlBox = new QGroupBox("End-effector control");
    SetleftDown->addWidget(EndControlBox);
    SetLeft->addLayout(SetleftDown);
    //模块四---点对点重复抓放
    QGroupBox *PointToPointBox = new QGroupBox("Automatic mode");
    SetLeft->addWidget(PointToPointBox);
    //模块五---摄像头视频显示
    QGroupBox *CameraBox = new QGroupBox("Camera video display");
    SetRight->addWidget(CameraBox);
    //模块六---目标空间位置
    QGroupBox *SpatialLocationBox = new QGroupBox("Target spatial location");
    SetRightDown->addWidget(SpatialLocationBox);
    //模块七---移动机械臂---点动控制
    QGroupBox *JogControlBox = new QGroupBox("Jog control");
    SetRightDown->addWidget(JogControlBox);
    // 将 SetRightDown 布局添加到 SetRight 布局中
    SetRight->addLayout(SetRightDown);
    // 将 SetLeft 和 SetRight 布局添加到 SetAll 布局中
    SetAll->addLayout(SetLeft);
    SetAll->addLayout(SetRight);
    // 将 SetAll 布局设置为主部件的布局
    this->setLayout(SetAll);
    //设置样式
    JogControlBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    SpatialLocationBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    CameraBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    PointToPointBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    EndControlBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    OperationBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");
    CommunicationBox->setStyleSheet("QGroupBox { border: 2px solid #53C7BB; color: #53C7BB;}");

    //模块一
    //两个标签、两个输入框、一个连接按钮
    QLabel *IpLab = new QLabel(this);
    IpLab->move(65, 55);
    IpLab->setStyleSheet("QLabel { font-size: 26px; font-weight: bold;color: #85D8D5;}");
    IpLab->setText("  Ip");
    QLabel *PortLab = new QLabel(this);
    PortLab->setStyleSheet("QLabel { font-size: 26px; font-weight: bold;color: #85D8D5;}");
    PortLab->setText("Port");
    PortLab->move(65, 135);
    this->IpEdit = new QLineEdit(this);
    this->PortEdit = new QLineEdit(this);
    this->Connect = new MyButton(this);
    this->IpEdit->move(145, 45);
    this->IpEdit->setFixedSize(295, 50);
    this->IpEdit->setStyleSheet("QLineEdit {  font-size: 24px; background-color: #BBF8EC;}");
    this->IpEdit->setAlignment(Qt::AlignCenter);
    QRegExpValidator* ip_val = new QRegExpValidator(QRegExp("((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"), this);
    this->IpEdit->setValidator(ip_val);
    this->PortEdit->move(145, 120);
    this->PortEdit->setFixedSize(295, 50);
    this->PortEdit->setStyleSheet("QLineEdit {  font-size: 24px; background-color: #BBF8EC;}");
    this->PortEdit->setAlignment(Qt::AlignCenter);
    this->Connect->setText("Connect");
    this->Connect->setStyleSheet("background-color: #27E4E4;");
    this->Connect->move(195, 195);
    this->Connect->setFixedSize(195, 45);
    //模块一的关联函数
    connect(this->Connect, &MyButton::clicked, this, &Widget::ConnectRobot);

    //模块二
    this->SelextMode = new QLabel(this);
    this->Mode = new QComboBox(this);
    //位置大小字体
    this->SelextMode->setText("Select the mode");
    this->SelextMode->move(70, 325);
    this->SelextMode->setFixedSize(200, 55);
    this->SelextMode->setStyleSheet("QLabel { font-size: 20px;color: #86F9F2;}");
    this->Mode->move(40, 400);
    this->Mode->setFixedSize(210, 55);
    this->Mode->setStyleSheet("QComboBox { font-size: 22px;background-color: #BBF8EC;font-color: #86F9F2;}");
    this->Mode->addItem("    User mode");
    this->Mode->addItem(" Privileged mode");
    QVariant blueColor(QColor("#BBF8EC"));
    this->Mode->setItemData(0, blueColor, Qt::ForegroundRole);
    this->Mode->setItemData(1, blueColor, Qt::ForegroundRole);
    //模块二的关联函数
    connect(this->Mode, &QComboBox::currentTextChanged, this, &Widget::ModeChange);


    //模块三
    this->Grab = new MyButton(this);
    this->Release = new MyButton(this);
    //位置大小字体
    this->Grab->setText("Grab");
    this->Grab->move(360, 325);
    this->Grab->setFixedSize(110, 55);
    this->Grab->setStyleSheet("background-color: #A2ECDA;");
    this->Release->setText("Release");
    this->Release->move(360, 420);
    this->Release->setFixedSize(110, 55);
    this->Release->setStyleSheet("background-color: #A2ECDA;");
    //模块三的关联函数
    connect(this->Grab, &QPushButton::clicked, this, &Widget::GrabClawSlot);
    connect(this->Release, &QPushButton::clicked, this, &Widget::ReleaseClawSlot);

    //模块四
    this->ModeOne = new MyButton(this);
    this->ModeOne->setStyleSheet("background-color: #17A1A6;");
    this->ModeTwo = new MyButton(this);
    this->ModeTwo->setStyleSheet("background-color: #17A1A6;");
    this->ModeThree = new MyButton(this);
    this->ModeThree->setStyleSheet("background-color: #17A1A6;");
    this->ModeFour = new MyButton(this);
    this->ModeFour->setStyleSheet("background-color: #17A1A6");
    this->ModeFive = new MyButton(this);
    this->ModeFive->setStyleSheet("background-color: #17A1A6;");
    this->ModeSix = new MyButton(this);
    this->ModeSix->setStyleSheet("background-color: #17A1A6;");
    this->ModeEnable = false;
    //位置大小字体
    this->ModeOne->setText("ModeOne");
    this->ModeOne->move(80, 545);
    this->ModeOne->setFixedSize(180, 50);
    this->ModeTwo->setText("ModeTwo");
    this->ModeTwo->move(295, 545);
    this->ModeTwo->setFixedSize(180, 50);
    this->ModeThree->setText("ModeThree");
    this->ModeThree->move(80, 620);
    this->ModeThree->setFixedSize(180, 50);
    this->ModeFour->setText("ModeFour");
    this->ModeFour->move(295, 620);
    this->ModeFour->setFixedSize(180, 50);
    this->ModeFive->setText("ModeFive");
    this->ModeFive->move(80, 695);
    this->ModeFive->setFixedSize(180, 50);
    this->ModeSix->setText("ModeSix");
    this->ModeSix->move(295, 695);
    this->ModeSix->setFixedSize(180, 50);
    //关联相关槽函数
    connect(this->ModeOne, &MyButton::clicked, this, &Widget::ModeoneStart);
    connect(this->ModeTwo, &MyButton::clicked, this, &Widget::ModetwoStart);

    //模块五
    this->VideoLabel = new QLabel(this);
    this->VideoLabel->setGeometry(575, 50, 500, 300);
    this->VideoThread = new PlayVideo;
    connect(this->VideoThread, &PlayVideo::SendVideo, this, &Widget::ShowVideo);
    connect(this->VideoThread, &PlayVideo::SendPoint, this, &Widget::RecvPotint);

    //模块六
    //xyz三个字的显示
    QLabel *LabX = new QLabel(this);
    LabX->move(575, 450);
    LabX->setStyleSheet("QLabel { font-size: 28px; font-weight: bold;color: #85D8D5;}");
    LabX->setText("X");
    QLabel *LabY = new QLabel(this);
    LabY->setStyleSheet("QLabel { font-size: 28px; font-weight: bold;color: #85D8D5;}");
    LabY->setText("Y");
    LabY->move(575, 525);
    QLabel *LabZ = new QLabel(this);
    LabZ->setStyleSheet("QLabel { font-size: 28px; font-weight: bold;color: #85D8D5;}");
    LabZ->setText("Z");
    LabZ->move(575, 600);
    //显示当前的xyz、按钮实例化
    this->RobotX = new QLineEdit(this);
    this->RobotY = new QLineEdit(this);
    this->RobotZ = new QLineEdit(this);
    this->RobotMotion = new MyButton(this);
    //位置、大小、字体
    this->RobotX->move(615, 435);
    this->RobotX->setFixedSize(175, 50);
    this->RobotX->setStyleSheet("QLineEdit { font-size: 24px; background-color: #BBF8EC;}");
    this->RobotX->setAlignment(Qt::AlignCenter);
    this->RobotY->move(615, 510);
    this->RobotY->setFixedSize(175, 50);
    this->RobotY->setStyleSheet("QLineEdit { font-size: 24px; background-color: #BBF8EC;}");
    this->RobotY->setAlignment(Qt::AlignCenter);
    this->RobotZ->move(615, 585);
    this->RobotZ->setFixedSize(175, 50);
    this->RobotZ->setStyleSheet("QLineEdit { font-size: 24px;  background-color: #BBF8EC;}");
    this->RobotZ->setAlignment(Qt::AlignCenter);
    this->RobotMotion->setText("Motion");
    this->RobotMotion->setStyleSheet("background-color: #17A1A6;");
    this->RobotMotion->move(615, 675);
    this->RobotMotion->setFixedSize(175, 55);

    //模块七
    // 实例化
    this->XUp = new MyButton(this);
    this->XDown = new MyButton(this);
    this->YUp = new MyButton(this);
    this->YDown = new MyButton(this);
    this->ZUp = new MyButton(this);
    this->ZDown = new MyButton(this);
    this->SliderLab = new QLabel(this);
    this->Slider = new QSlider(Qt::Horizontal, this);
    this->Slider->setMinimum(0);
    this->Slider->setMaximum(2);
    this->Slider->setTickInterval(1);
    this->Slider->setValue(1);
    this->Slider->move(945, 690);
    this->Slider->setFixedWidth(140);
    this->Slider->setTickPosition(QSlider::TicksBelow);
    this->SliderLab->move(875, 690);
    this->SliderLab->setText("Medium");
    // 位置等设置
    this->XUp->setText("X+");
    this->XUp->move(985, 440);
    this->XUp->setFixedSize(80, 50);
    this->XUp->setStyleSheet("background-color: #21B7B9;");
    this->XDown->setText("X-");
    this->XDown->move(865, 440);
    this->XDown->setStyleSheet("background-color: #B4F9F8;");
    this->XDown->setFixedSize(80, 50);
    this->YUp->setText("Y+");
    this->YUp->move(985, 515);
    this->YUp->setFixedSize(80, 50);
    this->YUp->setStyleSheet("background-color: #B4F9F8;");
    this->YDown->setText("Y-");
    this->YDown->move(865, 515);
    this->YDown->setFixedSize(80, 50);
    this->YDown->setStyleSheet("background-color: #21B7B9;");
    this->ZUp->setText("Z+");
    this->ZUp->move(985, 590);
    this->ZUp->setFixedSize(80, 50);
    this->ZUp->setStyleSheet("background-color: #21B7B9;");
    this->ZDown->setText("Z-");
    this->ZDown->move(865, 590);
    this->ZDown->setFixedSize(80, 50);
    this->ZDown->setStyleSheet("background-color: #B4F9F8;");
    this->SliderLab->setStyleSheet("color: #85D8D5;");

    //滑块以及对应的信号与槽
    //滑块对应三个DPI等级，分别为1mm，5mm，10mm，当滑块被移动时，改变json里DPI对应的值
    connect(this->Slider, &QSlider::valueChanged, this, [=]() {
        int value = this->Slider->value();
        switch (value) {
            case 0:
                this->SliderLab->setText("Low");
                this->TransmitJson.insert("DPI", QJsonValue(DPI_LOW));
                break;
            case 1:
                this->SliderLab->setText("Medium");
                this->TransmitJson.insert("DPI", QJsonValue(DPI_MEDIUM));
                break;
            case 2:
                this->SliderLab->setText("High");
                this->TransmitJson.insert("DPI", QJsonValue(DPI_HIGH));
                break;
            }
        });

    //调用默认之下为user模式，所以debug的按钮设置为不可按下
    this->DisableButton();

    // 单动模式下的6个按钮的单击
    connect(this->XDown, &MyButton::MyClicked, this, &Widget::XDownTillSlot);
    connect(this->XUp, &MyButton::MyClicked, this, &Widget::XUpTillSlot);
    connect(this->YDown, &MyButton::MyClicked, this, &Widget::YDownTillSlot);
    connect(this->YUp, &MyButton::MyClicked, this, &Widget::YUpTillSlot);
    connect(this->ZDown, &MyButton::MyClicked, this, &Widget::ZDownTillSlot);
    connect(this->ZUp, &MyButton::MyClicked, this, &Widget::ZUpTillSlot);

    // 单动模式下的6个按钮的按压
    connect(this->XDown, &MyButton::LongPress, this, &Widget::XDownEinmalSlot);
    connect(this->XUp, &MyButton::LongPress, this, &Widget::XUpEinmalSlot);
    connect(this->YDown, &MyButton::LongPress, this, &Widget::YDownEinmalSlot);
    connect(this->YUp, &MyButton::LongPress, this, &Widget::YUpEinmalSlot);
    connect(this->ZDown, &MyButton::LongPress, this, &Widget::ZDownEinmalSlot);
    connect(this->ZUp, &MyButton::LongPress, this, &Widget::ZUpEinmalSlot);

    // 单动模式下的6个按钮的松开
    connect(this->XDown, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    connect(this->XUp, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    connect(this->YDown, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    connect(this->YUp, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    connect(this->ZDown, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    connect(this->ZUp, &MyButton::MyReleased, this, &Widget::ButtonReleasedSlot);
    //xyz的发送按钮
    connect(this->RobotMotion,&MyButton::clicked,this,&Widget::ButtonXYZSlot);
}

Widget::~Widget()
{
    this->VideoThread->requestInterruption();  // 请求线程中止
    this->VideoThread->wait();  // 等待线程结束
}

void Widget::EnableButton()
{
    this->EnablePrivileged();
    this->Mode->setEnabled(true);
    this->ModeOne->setEnabled(true);
    this->ModeTwo->setEnabled(true);
    this->ModeThree->setEnabled(true);
    this->ModeFour->setEnabled(true);
    this->ModeFive->setEnabled(true);
    this->ModeSix->setEnabled(true);
}

void Widget::DisableButton()
{
    this->DisablePrivileged();
    this->Mode->setEnabled(false);
    this->ModeOne->setEnabled(false);
    this->ModeTwo->setEnabled(false);
    this->ModeThree->setEnabled(false);
    this->ModeFour->setEnabled(false);
    this->ModeFive->setEnabled(false);
    this->ModeSix->setEnabled(false);
}

void Widget::EnablePrivileged()
{
    this->XUp->setEnabled(true);
    this->XDown->setEnabled(true);
    this->YUp->setEnabled(true);
    this->YDown->setEnabled(true);
    this->ZUp->setEnabled(true);
    this->ZDown->setEnabled(true);
    this->Slider->setEnabled(true);
    this->Grab->setEnabled(true);
    this->Release->setEnabled(true);
    this->RobotX->setEnabled(true);
    this->RobotY->setEnabled(true);
    this->RobotZ->setEnabled(true);
    this->RobotMotion->setEnabled(true);
}

void Widget::DisablePrivileged()
{
    this->XUp->setEnabled(false);
    this->XDown->setEnabled(false);
    this->YUp->setEnabled(false);
    this->YDown->setEnabled(false);
    this->ZUp->setEnabled(false);
    this->ZDown->setEnabled(false);
    this->Slider->setEnabled(false);
    this->Grab->setEnabled(false);
    this->Release->setEnabled(false);
    this->RobotX->setEnabled(false);
    this->RobotY->setEnabled(false);
    this->RobotZ->setEnabled(false);
    this->RobotMotion->setEnabled(false);
}

#if 0   //这部分作为调试使用，不需要连接机械臂
void Widget::ConnectRobot()
{
    // 关联按钮相关的信号与槽
    // 发送默认数据
    this->TransmitJson.insert("Claw", QJsonValue(false)); //控制夹爪是否开启
    this->TransmitJson.insert("ClawFlag", QJsonValue(true)); //控制要不要启动功能模式
    this->TransmitJson.insert("States", QJsonValue("null")); //选择功能模式 ，till点击动一下 || einmal 按下一直动，直到松开 || null 直接输入坐标模式
    this->TransmitJson.insert("EinmalFlag", QJsonValue(false)); //标志位，确认einmal  true == 开始 || false == 停止
    this->TransmitJson.insert("Button", QJsonValue("XUp")); //部位， 确认要移动的位置
    this->TransmitJson.insert("DPI", QJsonValue(DPI_MEDIUM)); // 分辨率， 控制单次移动的距离
    this->Position.append(0); // 起始位置x坐标
    this->Position.append(0); // 起始位置y坐标
    this->Position.append(0); // 起始位置z坐标
    this->TransmitJson.insert("Position", this->Position);  //空间坐标轴数组
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << SendJson;
    QMessageBox::information(this, "提示", "<font size='26' color=#B4F9F8>连接机械臂成功</font>");
    this->EnableButton();
    this->DisablePrivileged();
    this->IpEdit->setEnabled(false);
    this->PortEdit->setEnabled(false);
    this->Connect->setEnabled(false);
}

#endif

#if 1 //这部分作为实际使用，需要连接机械臂
void Widget::ConnectRobot()
{
    // 搭建通信客户端
    this->TimeoutCheck = new QTimer(this);
    this->TimeoutCheck->start(5000);
    connect(this->TimeoutCheck, &QTimer::timeout, this, &Widget::ConnectError);
    this->MyTcp = new QTcpSocket(this);
    this->IpEdit->setText("192.168.157.8");
    this->PortEdit->setText("9596");
    this->MyTcp->connectToHost(this->IpEdit->text(), this->PortEdit->text().toUInt(), QIODevice::ReadWrite);
    connect(this->MyTcp, &QAbstractSocket::connected, this, &Widget::ConnectOk);
    connect(this->MyTcp, &QAbstractSocket::disconnected, this, [=](){
        QMessageBox::information(this, "提示", "<font size='26' color='red'>与机械臂连接已断开</font>");
    });
    //线程初始化、连接线程开始信号与对应的槽函数
    connect(&this->ReadMsgThread, &QThread::started, this, [=](){
        connect(this->MyTcp, &QAbstractSocket::readyRead, this,&Widget::ReadMsg);
    });
    ReadMsgThread.start();//启动线程

    // 关联按钮相关的信号与槽
    // 发送默认数据
    this->TransmitJson.insert("Claw", QJsonValue(false)); //控制夹爪是否开启
    this->TransmitJson.insert("ClawFlag", QJsonValue(true)); //控制要不要启动功能模式
    this->TransmitJson.insert("States", QJsonValue("null")); //选择功能模式 ，till点击动一下 || einmal 按下一直动，直到松开 || null 直接输入坐标模式
    this->TransmitJson.insert("EinmalFlag", QJsonValue(false)); //标志位，确认einmal  true == 开始 || false == 停止
    this->TransmitJson.insert("Button", QJsonValue("XUp")); //部位， 确认要移动的位置
    this->TransmitJson.insert("DPI", QJsonValue(DPI_MEDIUM)); // 分辨率， 控制单次移动的距离
    this->Position.append(0); // 起始位置x坐标
    this->Position.append(0); // 起始位置y坐标
    this->Position.append(0); // 起始位置z坐标
    this->TransmitJson.insert("Position", this->Position);  //空间坐标轴数组
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}
#endif

void Widget::ConnectError()
{
    this->TimeoutCheck->stop();
    QMessageBox::information(this, "警告", "<font size='26' color=#B4F9F8>连接机械臂超时，请重启并检查超时原因</font>");
    //QApplication::exit();
}

void Widget::ConnectOk()
{
    this->TimeoutCheck->stop();
    QMessageBox::information(this, "提示", "<font size='26' color=#B4F9F8>连接机械臂成功</font>");
    this->EnableButton();
    this->DisablePrivileged();
    this->IpEdit->setEnabled(false);
    this->PortEdit->setEnabled(false);
    this->Connect->setEnabled(false);

}

void Widget::ReadMsg()
{
    QByteArray msg = this->MyTcp->readAll();
    qDebug() << msg; // 显示
    QJsonDocument doc = QJsonDocument::fromJson(msg);
    if (!doc.isNull() && doc.isObject()) //数据有效
    {
        QJsonObject obj = doc.object();
        QString DataX = obj.value("X").toString();
        QString DataY = obj.value("Y").toString();
        QString DataZ = obj.value("Y").toString();
        this->RobotX->setText(QString::number(DataX.toInt()*10));
        this->RobotX->setText(QString::number(DataY.toInt()*10));
        this->RobotX->setText(QString::number(DataZ.toInt()*10));
        if(obj.value("End") == false)
        {
            //数据传输结束
        }
   }
   else
   {
            qDebug() << "Invalid JSON";
   }


}

void Widget::ModeChange(const QString &text)
{
    qDebug() << "emit currentTextChanged";
    if(text == "    User mode")
    {
        qDebug() << "user mode";
        this->DisablePrivileged();
    }
    else if(text == " Privileged mode")
    {
        qDebug() << "Privileged mode";
        this->EnablePrivileged();
    }
}

void Widget::GrabClawSlot()
{
    this->TransmitJson.insert("Claw", QJsonValue(true));
    this->TransmitJson.insert("ClawFlag", QJsonValue(false));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "Grab clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ReleaseClawSlot()
{
    this->TransmitJson.insert("Claw", QJsonValue(false));
    this->TransmitJson.insert("ClawFlag", QJsonValue(false));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "Release clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ModeoneStart()
{
    if(!(this->ModeEnable)) //如果未使能
    {
        this->DisableButton();
        this->ModeOne->setEnabled(true);
        this->ModeEnable = true;
        this->VideoThread->start();
    }
    else    //如果已经被使能
    {
        this->EnableButton();
        this->DisablePrivileged();
        this->ModeEnable = false;
        this->VideoThread->requestInterruption();
        this->VideoLabel->clear();
    }
}

void Widget::ModetwoStart()
{
    if(!(this->ModeEnable)) //如果未使能
    {
        this->DisableButton();
        this->ModeTwo->setEnabled(true);
        this->ModeEnable = true;
        this->TransmitJson.insert("States", QJsonValue("mode2"));
        this->TransmitJson.insert("ClawFlag", QJsonValue(true));
        QJsonDocument SendJson(this->TransmitJson);
        qDebug() << SendJson;
        this->MyTcp->write(SendJson.toJson() + "\r\n");
    }
    else //如果已经被使能
    {
        this->EnableButton();
        this->DisablePrivileged();
        this->ModeEnable = false;
        this->TransmitJson.insert("ClawFlag", QJsonValue(false));
        QJsonDocument SendJson(this->TransmitJson);
        qDebug() << SendJson;
        this->MyTcp->write(SendJson.toJson() + "\r\n");
    }
}

void Widget::ShowVideo(QImage image)
{
    this->VideoLabel->setPixmap(QPixmap::fromImage(image));
    this->VideoLabel->update();
}

void Widget::RecvPotint(QPoint point)
{
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("null"));
    this->Position.replace(0, point.x());   //获得x坐标
    this->Position.replace(1, point.y());   //获得y坐标
    this->Position.replace(2, 0);   //z坐标提前写好
    //发送捕获的坐标信息
    this->TransmitJson.insert("Position", this->Position);  //空间坐标轴数组
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::XUpTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("XUp"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "XUp clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::XDownTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("XDown"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "XDown clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::YUpTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("YUp"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "YUp clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::YDownTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("YDown"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "YDown clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ZUpTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("ZUp"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "ZUp clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ZDownTillSlot()
{
    // 编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("till"));
    this->TransmitJson.insert("Button", QJsonValue("ZDown"));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "ZDown clicked" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::XUpEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("XUp"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "XUp longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::XDownEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("XDown"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "XDown longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::YUpEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("YUp"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "YUp longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::YDownEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("YDown"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "YDown longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ZUpEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("ZUp"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "ZUp longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ZDownEinmalSlot()
{
    //编辑json数据
    this->TransmitJson.insert("ClawFlag", QJsonValue(true));
    this->TransmitJson.insert("States", QJsonValue("einmal"));
    this->TransmitJson.insert("Button", QJsonValue("ZDown"));
    this->TransmitJson.insert("EinmalFlag", QJsonValue(true));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "ZDown longPress" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ButtonReleasedSlot()
{
    this->TransmitJson.insert("EinmalFlag", QJsonValue(false));
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "longPress released" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}

void Widget::ButtonXYZSlot()
{
    this->Position[0] = this->RobotX->text(); // x坐标
    this->Position[1] = this->RobotY->text(); // y坐标
    this->Position[2] = this->RobotZ->text(); // z坐标
    this->TransmitJson.insert("Position", this->Position);  //空间坐标轴数组
    QJsonDocument SendJson(this->TransmitJson);
    qDebug() << "RobotXYZ released" << SendJson;
    this->MyTcp->write(SendJson.toJson() + "\r\n");
}
